import { takeEvery } from "redux-saga/effects";
import { NATIVE_CLASS } from "@common/constants";
import { callNativeFunc } from "@common/saga-helper";
import {
  SAGA_SALE_OUTBOUND_LOAD_OPS_FORM_DATA,
  SAGA_SALE_OUTBOUND_STASH_OPS_DATA,
  SAGA_SALE_OUTBOUND_FINISH_OPS,
  SAGA_SALE_OUTBOUND_LOAD_ITEM_DETAILS,
  SAGA_SALE_OUTBOUND_LOAD_BARCODE_DETAILS,
  SAGA_SALE_OUTBOUND_LOAD_OUTBOUND_TASK,
  SAGA_SALE_OUTBOUND_DEL_BARCODE,
  SAGA_SALE_OUTBOUND_PUSH_2_SERVER
} from "./saga-types";
import {
  SET_SALE_OUTBOUND_OPS_FORM_DATA,
  SET_SALE_OUTBOUND_ITEM_DETAILS,
  SET_SALE_OUTBOUND_BARCODE_DETAILS,
  SET_SALE_OUTBOUND_TASK_LIST,
  DEL_SALE_OUTBOUND_BARCODE
} from "./action-types";

function* loadSaleOutboundOpsFormDataSaga() {
  yield takeEvery(SAGA_SALE_OUTBOUND_LOAD_OPS_FORM_DATA, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.SaleOutboundDetailsHandler,
            action: "loadOpsFormData",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_SALE_OUTBOUND_OPS_FORM_DATA],
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* stashSaleOutboundOpsDataSaga() {
  yield takeEvery(SAGA_SALE_OUTBOUND_STASH_OPS_DATA, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.SaleOutboundOpsHandler,
            action: "create",
            needNonce: false,
            params: action.payload,
          },
          callback: action.payload.callback,
        },
        {
          payload: {
            classFullName: NATIVE_CLASS.SaleOutboundDetailsHandler,
            action: "loadOpsFormData",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_SALE_OUTBOUND_OPS_FORM_DATA],
        },
      ],
    });
  });
}

function* finishSaleOutboundOpsSaga() {
  yield takeEvery(SAGA_SALE_OUTBOUND_FINISH_OPS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.SaleOutboundOpsHandler,
            action: "finish",
            needNonce: false,
            params: action.payload,
          },
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* loadSaleOutboundItemDetailsSaga() {
  yield takeEvery(SAGA_SALE_OUTBOUND_LOAD_ITEM_DETAILS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.SaleOutboundOpsHandler,
            action: "loadItemDetails",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_SALE_OUTBOUND_ITEM_DETAILS],
        },
      ],
    });
  });
}

function* loadSaleOutboundBarcodeDetailsSaga() {
  yield takeEvery(SAGA_SALE_OUTBOUND_LOAD_BARCODE_DETAILS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.SaleOutboundOpsHandler,
            action: "loadBarcodeDetails",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_SALE_OUTBOUND_BARCODE_DETAILS],
        },
      ],
    });
  });
}

function* loadSaleOutboundTaskListSaga() {
  yield takeEvery(SAGA_SALE_OUTBOUND_LOAD_OUTBOUND_TASK, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.SaleOutboundDetailsHandler,
            action: "loadTaskDetails",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_SALE_OUTBOUND_TASK_LIST],
        },
      ],
    });
  });
}

function* delSaleOutboundBarcodeSaga() {
  yield takeEvery(SAGA_SALE_OUTBOUND_DEL_BARCODE, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.SaleOutboundOpsHandler,
            action: "delBarcode",
            needNonce: false,
            params: action.payload,
          },
          respActions: [DEL_SALE_OUTBOUND_BARCODE],
          callback: action.payload.callback
        },
      ],
    });
  });
}

function* pushSaleOutbound2ServerSaga() {
  yield takeEvery(SAGA_SALE_OUTBOUND_PUSH_2_SERVER, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.SaleOutboundHandler,
            action: "push2Server",
            needNonce: true,
            params: action.payload,
          },
          callback: action.payload.callback
        },
      ],
    });
  });
}

export default [
  loadSaleOutboundOpsFormDataSaga(),
  stashSaleOutboundOpsDataSaga(),
  finishSaleOutboundOpsSaga(),
  loadSaleOutboundItemDetailsSaga(),
  loadSaleOutboundBarcodeDetailsSaga(),
  loadSaleOutboundTaskListSaga(),
  delSaleOutboundBarcodeSaga(),
  pushSaleOutbound2ServerSaga()
];
